﻿using System;
using CodeMinder.Core;

namespace CodeMinder.Output.MotionChart.Entities
{
    public class MotionChart : IScriptable
    {
        #region Fields
        private readonly Options _options;
        private readonly DataTable _dataTable;
        private readonly string _containerId;
        #endregion

        #region Ctor
        public MotionChart(Options options, DataTable dataTable, string containerIdForChart)
        {
            Assure.ThatArgument(() => dataTable, p => p.Columns.Count >= 3, "There should be at least 3 columns in the dataTable.");
            Assure.ThatArgument(() => dataTable, p => p.Columns[0].DataType == typeof(string), "The first column must be of type 'string'.");
            Assure.ThatArgument(() => dataTable, p => p.Columns[1].DataType == typeof(DateTime), "The second column must be of type 'DateTime'.");
            Assure.ThatArgument(() => options, p => p != null);
            Assure.ThatArgument(() => containerIdForChart, p => !string.IsNullOrEmpty(p));

            _options = options;
            _dataTable = dataTable;
            _containerId = containerIdForChart;
        }
        #endregion

        #region IScriptable Members

        public string ToScript()
        {
            const string chartFormat = 
@"function drawChart() {{
{0}
{1}
var container = document.getElementById('{2}');
var motionchart = new google.visualization.MotionChart(container);
motionchart.draw(data, options);
}}";

            return string.Format(chartFormat, _dataTable.ToScript(), _options.ToScript(), _containerId);
        }

        #endregion
    }
}